package Jona;

public class Conditional extends Statement {
	
	protected ArgObject antecedent;
	protected ArgObject consequent;
	private boolean Antecedent_isRecursive = false;
	

	
/* ********** CONSTRUCTORS *************************************** */
	
	/**
	 * Creates a new conditional "antecedent -> consequent"
	 * @param antecedent Antecedent of the conditional
	 * @param consequent Consequent of the conditional
	 */
	public Conditional(ArgObject antecedent, ArgObject consequent) {	
		super(ArgObject.ASSUMPTION, "");
		setConsequent(consequent);
		setAntecedent(antecedent);		
	}
	
	/**
	 * Creates a new conditional "antecedent -> consequent" with the given
	 * supporter and attacker relations
	 * @param antecedent Antecedent of the conditional
	 * @param consequent Consequent of the conditional
	 * @param supporters Array of the conditional's supporters
	 * @param attackers Array of the conditional's attackers
	 */
	public Conditional(ArgObject antecedent, ArgObject consequent, ArgObject[] supporters, ArgObject[] attackers) {	
		super(ArgObject.ASSUMPTION, "", supporters, attackers);
		setAntecedent(antecedent);
		setConsequent(consequent);
	}
	
	public Conditional(int argumentType,ArgObject consequent) {
		super(argumentType,"");
		setConsequent(consequent);
	}

	public Conditional(int argumentType, Statement antecedent, Statement supporter,Statement attacker) {	
		super(argumentType, "", new Statement[]{supporter},new Statement[]{attacker});
		setAntecedent(antecedent);		
	}
	
	public Conditional(int argumentType, Statement antecedent, Statement[] supporters, Statement[] attackers) {	
		super(argumentType, "", supporters, attackers);
		setAntecedent(antecedent);
	}


	

	
	
/* ********** GETTERS ******************************************** */
	
	/**
	 * Returns the antecedent
	 * @return the antecedent of this conditional
	 */
	public ArgObject getAntecedent() {
		return antecedent;
	}

	/**
	 * Returns the consequent
	 * @return the consequent of this conditional
	 */
	public ArgObject getConsequent() {
		return consequent;
	}
	
	public String getUnparsedText()
	{
		String rt;
		rt = "if (";
		if (antecedent != null) {
			rt += antecedent.getUnparsedText();
		}
		rt += ") then (";
		if (consequent != null) {
			rt += consequent.getUnparsedText();
		}
		return rt+")";		
	}

	public String getText(){
		String rt;
		rt = "if (";
		if (antecedent != null) {
			rt += antecedent.getText();
		}
		rt += ") then (";
		if (consequent != null) {
			rt += consequent.getText();
		}
		return rt+")";
	}
	

	
	
/* ********** SETTERS ******************************************** */

	/**
	 * Resets the antecedent to a new statement, and makes sure the
	 * supporter relation of the consequent are updated.
	 * @param newAntecedent Statement to set as antecedent
	 */
	public void setAntecedent(ArgObject newAntecedent){
		if (consequent != null) consequent.removeSupportingConditional(this);
		antecedent = newAntecedent;
		if (consequent != null) consequent.addSupportingConditional(this);
	}

	/**
	 * Resets the consequent to a new statement, and makes sure the
	 * supporter relation of the previous and new consequent are updated.
	 * @param newConsequent Statement to set as consequent
	 */
	public void setConsequent(ArgObject newConsequent) {
		if (consequent != null) consequent.removeSupportingConditional(this);
		consequent = newConsequent;
		if (consequent != null) consequent.addSupportingConditional(this);
	}
	



	
/* ********** CONTENT METHODS ************************************ */
	
	public ArgObject giveCopy() {
		Conditional rt = new Conditional(antecedent, consequent, supportConditionals, attackConditionals);		
		
		return rt;
	}	

	
	public boolean containsAntecedent(ArgObject ao1)
	{
		if (antecedent != null) {
			if (antecedent.equals(ao1)) {
				return true;
			} else if (antecedent instanceof Conjunct) {
				return ((Conjunct)antecedent).contains(ao1);
			}
		}
		return false;
	}
	
	
	/**
	 * Kept for backwards compatibility
	 * And will be kept because it's still used!
	 * @see Conditional.getAntecedent()
	 */
	public ArgObject[] getAntecedents() {
		if (antecedent instanceof Conjunct)
			return ((Conjunct)antecedent).getOperands();
		else if (antecedent != null) {
			ArgObject[] tmp = new ArgObject[1];
			tmp[0] = antecedent;
			return tmp;
		}
		return new ArgObject[0];
	}
	
	public void addAntecedent(ArgObject statement) {
		if (antecedent == null) antecedent = new Conjunct();
		((Conjunct)antecedent).addOperand(statement);
	}

	public void removeAntecedent(ArgObject statement)
	{
		if (antecedent instanceof Conjunct)
			((Conjunct)antecedent).removeOperand(statement);
		else if (antecedent != null) 
		{
			antecedent = null;
		}
	}
	

	public String toString()
	{
		ArgObject [] antecedents = getAntecedents();
		String rt = "If (";
		for(int i =0; i < (antecedents.length -1); i++)
		{
			rt+= antecedents[i].toString() + ",";
		}
		rt+= antecedents[antecedents.length -1].toString() + ") then " + consequent.toString();		
		return rt;
	}

	public boolean isAntecedent_isRecursive() {
		return Antecedent_isRecursive;
	}

	public void setAntecedent_isRecursive(boolean antecedent_isRecursive) {
		Antecedent_isRecursive = antecedent_isRecursive;
	}

}

